package com.haha.helloworld;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

public class ProgressActivity extends AppCompatActivity {
    private ProgressBar pd_3;
    private Button btn_start;
    private Button btn_progress_dialog_1;
    private Button btn_progress_dialog_2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress);
        pd_3= (ProgressBar)findViewById(R.id.pd_3);
        btn_start= (Button)findViewById(R.id.btn_start);
        btn_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int progress=0;
                while (progress<=100){
                    progress++;
                    pd_3.setProgress(progress);

                }

            }
        });
        btn_progress_dialog_1= (Button)findViewById(R.id.btn_progress_dialog_1);
        btn_progress_dialog_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProgressDialog progressDialog=new ProgressDialog(ProgressActivity.this);
                progressDialog.setTitle("提示");
                progressDialog.setMessage("正在加载");
                //被取消的时候执行
                progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        Toast.makeText(getApplicationContext(), "已取消." , Toast.LENGTH_SHORT).show();
                    }
                });
                //此加载不可以被取消
//                progressDialog.setCancelable(false);
                progressDialog.show();
            }
        });
        btn_progress_dialog_2= (Button)findViewById(R.id.btn_progress_dialog_2);
        btn_progress_dialog_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProgressDialog progressDialog=new ProgressDialog(ProgressActivity.this);
                //设置进度条的样式
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.setTitle("提示");
                progressDialog.setMessage("正在加载");
                //progressDialog也是可以设置按钮的，演示一下
                progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "nice", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(), "不错！." , Toast.LENGTH_SHORT).show();
                    }
                });
                progressDialog.show();
            }
        });
    }


}